package com.zhilianbao.leyaogo.view.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.bql.animationcheckbox.AnimationCheckBox;
import com.bql.convenientlog.CLog;
import com.bql.utils.NumberUtlis;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.constants.IntentConfig;
import com.zhilianbao.leyaogo.constants.PayConfig;
import com.zhilianbao.leyaogo.view.widgets.MoneyTextView;

/**
 * Author: hcy
 * Description:确认支付，支付详情Dialog
 * Created: 2016/11/4 13:50
 */
public class ConfirmPayDialog extends DialogFragment implements View.OnClickListener {

    //    private String totalPrice, balanceDisCount, needPay;

    private int payType;

    private MoneyTextView mTotalMt, mBalanceMt, mNeedMt;
    private TextView mPayTypeTv;
    private AnimationCheckBox mCheckBox;

    //总价
    private double totalPrice;

    //余额
    private double availableBalance;

    //需付款
    private double needToPayPrice;


    //余额抵扣checkBox选中状态
    private boolean mCheckBoxState = true;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            Bundle bundle = getArguments();
            payType = bundle.getInt(IntentConfig.PAY_TYPE, PayConfig.WX_PAY);
            totalPrice = bundle.getDouble(IntentConfig.PAY_TOTAL_PRICE, 0);
            availableBalance = bundle.getDouble(IntentConfig.PAY_AVAILABLE_BALANCE, 0);
            needToPayPrice = bundle.getDouble(IntentConfig.PAY_NEED_TO_PAY_PRICE, 0);
            mCheckBoxState = bundle.getBoolean(IntentConfig.BALANCE_CHECKBOX_STATE, true);
            CLog.i("hcy", "payType=" + payType);
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.Dialog);
        View view = LayoutInflater.from(getActivity()).inflate(
                R.layout.dialog_confirm_pay, null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.windowAnimations = R.style.DialogAnimation;
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);
        initView(view);
        return dialog;
    }

    /**
     * 初始化
     *
     * @param view
     */
    public void initView(View view) {
        mBalanceMt = (MoneyTextView) view.findViewById(R.id.mt_discount);
        mTotalMt = (MoneyTextView) view.findViewById(R.id.mt_total_account);
        mNeedMt = (MoneyTextView) view.findViewById(R.id.mt_need_pay);
        mPayTypeTv = (TextView) view.findViewById(R.id.tv_pay_method);
        mCheckBox = (AnimationCheckBox) view.findViewById(R.id.anim_check_box);
        mCheckBox.setChecked(mCheckBoxState, false);
        mTotalMt.setAmount(totalPrice);
        mBalanceMt.setAmount(availableBalance);
        mBalanceMt.setSymbol(mCheckBoxState ? ("-" + getString(R.string.price_symbol)) : getString(R.string.price_symbol));
        mNeedMt.setAmount(needToPayPrice);
        mCheckBox.setOnCheckedChangeListener((checkBox, isChecked) -> {
            if (isChecked) {
                mNeedMt.setAmount(NumberUtlis.sub(totalPrice, availableBalance));
            } else {
                mNeedMt.setAmount(totalPrice);
            }
            mBalanceMt.setSymbol(isChecked ? ("-" + getString(R.string.price_symbol)) : getString(R.string.price_symbol));
            if (mOnPayTypeSelectListener != null) {
                mOnPayTypeSelectListener.onCheckChanged(checkBox, isChecked, isChecked ? NumberUtlis.sub(totalPrice, availableBalance) : totalPrice);
            }
        });
        mPayTypeTv.setOnClickListener(this);
        view.findViewById(R.id.iv_dismiss).setOnClickListener(this);
        view.findViewById(R.id.tv_confirm_pay).setOnClickListener(this);
        view.findViewById(R.id.fl_acb).setOnClickListener(this);
        CLog.i("hcy", "switch--payType=" + payType);
        switch (payType) {
            case PayConfig.WX_PAY:
                mPayTypeTv.setText(getString(R.string.pay_detail_wx_pay));
                break;
            case PayConfig.ALI_PAY:
                mPayTypeTv.setText(getString(R.string.pay_detail_ali_pay));
                break;
            case PayConfig.YUE_PAY:
                mPayTypeTv.setText(getString(R.string.pay_detail_yue));
                break;
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_dismiss:
                this.dismiss();
                break;
            case R.id.tv_pay_method://选择付款方式
                if (mOnPayTypeSelectListener != null) {
                    mOnPayTypeSelectListener.onPayTypeClick();
                }
                break;
            case R.id.tv_confirm_pay://确认付款
                if (mOnPayTypeSelectListener != null) {
                    mOnPayTypeSelectListener.onConfirmClick();
                }
                break;
            case R.id.fl_acb:
                mCheckBox.setChecked(!mCheckBox.isChecked(), true);
                break;
        }
    }

    /**
     * 点击确认支付，选择支付方式回调接口
     */
    public interface OnMultiClickListener {

        void onConfirmClick();

        void onPayTypeClick();

        void onCheckChanged(AnimationCheckBox checkBox, boolean isChecked, double needToPay);
    }

    private OnMultiClickListener mOnPayTypeSelectListener;

    public void setPayTypeSelectListener(OnMultiClickListener listener) {
        mOnPayTypeSelectListener = listener;
    }
}
